<?php
// +----------------------------------------------------------------------
// | Quotes [未来可期]
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: 夸克广告自助系统 https://support.qq.com/products/129921/ 作者QQ：79517721
// +----------------------------------------------------------------------
// | Date: 2022年3月5日
// +----------------------------------------------------------------------


include("./app/common.php");
$act = isset($_GET['act']) ? daddslashes($_GET['act']) : null;

@header('Content-Type: application/json; charset=UTF-8');
$today = date("Y-m-d");//当天时间

switch ($act) {

    case 'pay':
        $data = isQ();
        $ad_qq = $data['ad_qq'];
        $ad_title = $data['ad_title'];
        $ad_url = $data['ad_url'];
        $ad_img = $data['ad_img'];
        $ad_type = $data['ad_type'];
        $id = $data['id'];
        $code = $data['code'];
        $month = $data['month'];

        $ad_title = trim(strip_tags(daddslashes($ad_title)));
        $ad_url = trim(strip_tags(daddslashes($ad_url)));
        $ad_img = trim(strip_tags(daddslashes($ad_img)));

        if(!is_numeric($month) || !is_numeric($id)){
            responseResult(0,"请返回首页重新操作,错误码00000");
        }
        if($_SESSION['vc_user_code'] != $code){
            responseResult(0,"验证码错误,请点击验证码刷新");
        }

        if(!isset($data['ad_type']) || !isset($data['id']) ){
            responseResult(0,"请返回首页重新操作,错误码10000");
        }

        $adArr = ['big','small','word'];//数据表 数组
        if(!in_array(trim($data['ad_type']),$adArr)){
            responseResult(0,"请返回首页重新操作,错误码10001");
        }

       if(($data['ad_type'] == 'big' || $data['ad_type'] == 'small') && (!isset($data['ad_img'])|| !isset($data['ad_url']) ) ){
           responseResult(0,"请将信息填写完整,错误码10002");
       }

        if(($data['ad_type'] == 'word') && (!isset($data['ad_title']) || !isset($data['ad_url']) ) ){
            responseResult(0,"请将信息填写完整,错误码10003");
        }

        if(!isset($data['ad_qq'])  ){
            responseResult(0,"请将信息填写完整,错误码10004");
        }

        //对信息校验
        if (strlen($ad_qq) < 5 || !preg_match('/^[0-9]+$/',$ad_qq)) {
            responseResult(0,"请输入正确的QQ,错误码10005");
        } else if (!strexists($ad_url,"http://") && !strexists($ad_url,"https://")) {
            responseResult(0,"广告链接需要带http://或者https://,错误码10006");
        } /*else if (!strexists($ad_img,"http://") && !strexists($ad_img,"https://")) {
            responseResult(0,"图片链接需要带http://或者https://,错误码10007");
        }*/

        $thisAd = $ADDB->query("SELECT * FROM ad_{$ad_type} where id = {$id}  limit 1")->fetch();
        if(empty($thisAd) || $today < $thisAd['end_time']){
            responseResult(0,"该广告位暂时处于保护状态,错误码10008");
        }


        $payType = trim(strip_tags(daddslashes($data['payType'])));

        if(!in_array($payType,array('wxpay','alipay'))){
            responseResult(0,"支付方式错误,错误码10009");
        }

        if(!in_array($month,array('1','2','3','4','5','6'))){
            responseResult(0,"购买时间错误,错误码10011");
        }

        $order_no = date("YmdHis") . rand(111, 999);
        $date = date("Y-m-d H:i:s");

        $account = $thisAd['price'] * $month;
        $name = '广告位购买';
        // 广告类型中文|广告类型|广告位置| 广告商QQ | 广告链接 | 广告标题 | 图片链接 | 购买月数
        // small  |     1 | 79517721|
        $des = getAdName($ad_type).'|'.$ad_type.'|'.$thisAd['id'].'|'.$ad_qq.'|'.$ad_url.'|'.$ad_title.'|'.$ad_img.'|'.$month;
        $sitename = $config['site_name'];//站点名字
        $ip = getIp();

        $sql = $ADDB->exec("INSERT INTO `ad_order` (`order_no`,  `name`, `ip`, `money`, `date`, `type`,  `status` ,`des`)VALUES ('{$order_no}', '{$name}', '{$ip}',
 
 '{$account}', '{$date}','{$payType}','0','{$des}')");
        if($sql){
            $data = [];
            $data['order_no'] = $order_no;
            $data['money'] = $account;
            $data['type'] = $payType;
            responseResult(1,'生成订单成功',$data);
        }else{
            responseResult(0,"生成订单失败");
        }


        break;


    case 'image':
        $image='https://api.ixiaowai.cn/gqapi/gqapi.php';
        $result=array("code"=>1,"image"=>$image);
        exit(json_encode($result));
        break;



    /**
     * author: 79517721@qq.com
     * time:2020/2/12 22:35
     * description:TODO 用户登录
     */
    case 'login':
        if (isset($_POST['username']) && isset($_POST['username'])) {
            $username = trim(strip_tags(daddslashes($_POST['username'])));
            $password = trim(strip_tags(daddslashes($_POST['password'])));
            $username = strtolower($username);

            $code = trim(strip_tags(daddslashes($_POST['code'])));

            if($_SESSION['vc_user_code'] != $code){
                responseResult(0,"验证码错误,请点击验证码刷新");
            }

            $userrow = $ADDB->query("SELECT * FROM ad_user WHERE is_admin = '1' and `username`='{$username}' limit 1")->fetch();
            $password = md5($password);
            if ($username == $userrow['username'] && $password == $userrow['password']) {

                $session = md5($username . $password . $password_hash);
                $expiretime = time() + 6048000;
                $token = authcode("{$username}\t{$session}\t{$expiretime}", 'ENCODE', SYS_KEY);

                setcookie("admin_token", $token, time() + 604800);
                exit('{"code":1,"msg":"尊敬的 管理员 ,登录成功!"}');
            } else {
                exit('{"code":-1,"msg":"账号或密码不正确"}');
            }
        } else {
            exit('{"code":-1,"msg":"数据不完整"}');
        }
        break;

    /**
     * author: 79517721@qq.com
     * time:2020/2/12 22:35
     * description:TODO 用户退出系统
     */
    case 'logout':
        setcookie("user_token", "", time() - 604800);
        setcookie("admin_token", "", time() - 604800);
        exit('{"code":1,"msg":"退出成功"}');
        break;

    default:

        break;


}

?>